#!/bin/bash

if [[ $# -ne 2 ]]; then
    echo "The format is as follows:"
    echo "bash hmmer-h3-arm.sh buildpath installpath"
    echo ":param buildpath: 应用构建绝对路径"
    echo ":param installpath: 应用安装绝对路径"
    exit 1
fi

[[ ! "$1" =~ ^/.* || ! -d "$1" || "$1" = "/" ]] && echo "请输入正确的构建路径" && exit 1
[[ ! "$2" =~ ^/.* || ! -d "$2" || "$2" = "/" ]] && echo "请输入正确的安装路径" && exit 1

buildpath=$1
installpath=$2
[[ "$buildpath" =~ .*/$ ]] && buildpath=${buildpath%/*}
[[ "$installpath" =~ .*/$ ]] && installpath=${installpath%/*}
[[ "$buildpath" == "$installpath" ]] && echo "构建路径与安装路径需为不同路径" && exit 1
set -e
yum install -y environment-modules cmake

cd $buildpath
wget -t 40 -c https://github.com/EddyRivasLab/hmmer/archive/refs/heads/h3-arm.zip
unzip h3-arm.zip && rm -rf h3-arm.zip
cd hmmer-h3-arm
wget -t 40 -c https://github.com/EddyRivasLab/easel/archive/refs/heads/develop.zip
unzip develop.zip && rm -rf develop.zip
mv easel-develop/ easel/
cd easel
autoconf
./configure --prefix=$installpath/hmmer/easel CC=clang CXX=clang++
make -j$(nproc)
make check
make install
cd ..
sed -i '/AC_PREREQ/s/7/6/' configure.ac
autoconf
./configure --prefix=$installpath/hmmer CC=clang CXX=clang++
make
make check
make install
rm -rf $buildpath/hmmer-h3-arm
cat >"$installpath/hmmer/hmmer_modulefiles" <<EOF
#%Module1.0
conflict hmmer
variable modfile [file normalize [info script]]
proc getModulefileDir {} {
    variable modfile
        set modfile_path [file dirname \$modfile]
            return \$modfile_path
    }
set pwd [getModulefileDir]
set HMMER \$pwd
setenv HMMER \$HMMER
prepend-path PATH \$HMMER/bin
prepend-path PATH \$HMMER/easel/bin
EOF
